package com.liangfeng.readnews.util;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

/**
 * 消息提示
 * 
 * @author huangzhongwen
 * 
 */
public class ToastUtils {

    private ToastUtils() {

    }

    /**
     * 
     * @param context
     * @param text
     */
    public static void showShort(Context context, String text) {
        showShort(context, text, Gravity.CENTER);
    }


    /** 之前显示的内容 */
    private static String oldMsg ;
    /** Toast对象 */
    private static Toast toast = null ;
    /** 第一次时间 */
    private static long oneTime = 0 ;
    /** 第二次时间 */
    private static long twoTime = 0 ;

    /**
     * 
     * @param context
     * @param text
     * @param gravity
     * @param xOffset
     * @param yOffset
     */
    public static void showShort(Context context, String text,
                                 int gravity, int xOffset, int yOffset) {
        /*Toast t = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        t.setGravity(gravity, xOffset, yOffset);
        t.show();*/
        if(toast == null){
            toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
            toast.setGravity(gravity, xOffset, yOffset);
            toast.show() ;
            oneTime = System.currentTimeMillis() ;
        }else{
            twoTime = System.currentTimeMillis() ;
            if(text.equals(oldMsg)){
                if(twoTime - oneTime > Toast.LENGTH_SHORT){
                    toast.setGravity(gravity, xOffset, yOffset);
                    toast.show() ;
                }
            }else{
                oldMsg = text ;
                toast.setText(text) ;
                toast.setGravity(gravity, xOffset, yOffset);
                toast.show() ;
            }
        }
        oneTime = twoTime ;
    }





    public static void showShort2(Context context, String text,
                                 int gravity, int xOffset, int yOffset) {
        Toast t = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        t.setGravity(gravity, xOffset, yOffset);
        t.show();
    }


    /**
     * 
     * @param context
     * @param text
     * @param gravity
     */
    public static void showShort(Context context, String text, int gravity) {
        showShort(context, text, gravity, 0, 0);
    }

    /**
     * 
     * @param context
     * @param resId
     */
    public static void showShort(Context context, int resId) {
        showShort(context, resId, Gravity.CENTER);
    }

    /**
     * 
     * @param context
     * @param resId
     * @param gravity
     * @param xOffset
     * @param yOffset
     */
    public static void showShort(Context context, int resId, int gravity,
                                 int xOffset, int yOffset) {
        showShort(context, context.getString(resId), gravity, xOffset, yOffset);
    }

    /**
     * 
     * @param context
     * @param resId
     * @param gravity
     */
    public static void showShort(Context context, int resId, int gravity) {
        showShort(context, resId, gravity, 0, 0);
    }

    /**
     * 
     * @param context
     * @param text
     */
    public static void showLong(Context context, CharSequence text) {
        showLong(context, text, Gravity.CENTER);
    }

    /**
     * 
     * @param context
     * @param text
     * @param gravity
     * @param xOffset
     * @param yOffset
     */
    public static void showLong(Context context, CharSequence text,
                                int gravity, int xOffset, int yOffset) {
        Toast t = Toast.makeText(context, text, Toast.LENGTH_LONG);
        t.setGravity(gravity, xOffset, yOffset);
        t.show();
    }

    /**
     * 
     * @param context
     * @param text
     * @param gravity
     */
    public static void showLong(Context context, CharSequence text, int gravity) {
        showLong(context, text, gravity, 0, 0);
    }

    /**
     * 
     * @param context
     * @param resId
     */
    public static void showLong(Context context, int resId) {
        showLong(context, resId, Gravity.CENTER);
    }

    /**
     * 
     * @param context
     * @param resId
     * @param gravity
     * @param xOffset
     * @param yOffset
     */
    public static void showLong(Context context, int resId, int gravity,
                                int xOffset, int yOffset) {
        showLong(context, context.getString(resId), gravity, xOffset, yOffset);
    }

    /**
     * 
     * @param context
     * @param resId
     * @param gravity
     */
    public static void showLong(Context context, int resId, int gravity) {
        showLong(context, resId, gravity, 0, 0);
    }

}
